import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const changeEnvVariables = (vName, vVal) => {
  const envPath = path.resolve(__dirname, "../.env");
  const envContent = fs.readFileSync(envPath, "utf8");
  const env = dotenv.parse(envContent);
  env[vName] = vVal;
  let str = "";
  Object.keys(env).forEach(key => {
    const value = env[key];
    str += `${key}=${value}\n`;
  });
  fs.writeFileSync(envPath, str, "utf8");
};

export default changeEnvVariables;
